package zhongjie;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

// 测试类
public class ChatRoomTest {
    public static void main(String[] args) {
        // 启动服务器
        ChatServer server = new ChatServer();
        
        try {
            // 模拟客户端连接
            simulateCommonMember("user1");
            Thread.sleep(1000);
            simulateDiamondMember("user2");
            Thread.sleep(1000);
            
            // 模拟发送文本消息
            simulateSendTextMessage("user1", "user2", "你好，我是普通会员！");
            Thread.sleep(1000);
            
            // 模拟发送包含不雅词汇的文本消息
            simulateSendTextMessage("user2", "user1", "你这个垃圾，真笨！");
            Thread.sleep(1000);
            
            // 模拟普通会员发送图片（应该被拒绝）
            simulateSendImageMessage("user1", "user2", new byte[1024]);
            Thread.sleep(1000);
            
            // 模拟钻石会员发送小图片
            simulateSendImageMessage("user2", "user1", new byte[512 * 1024]); // 512KB
            Thread.sleep(1000);
            
            // 模拟钻石会员发送大图片（超过限制）
            simulateSendImageMessage("user2", "user1", new byte[2 * 1024 * 1024]); // 2MB
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭服务器
            server.shutdown();
        }
    }
    
    private static void simulateCommonMember(String userId) throws IOException {
        Socket socket = new Socket("localhost", 8888);
        ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
        out.writeObject(new Message(Message.REGISTER, userId, null, "common"));
        
        // 启动接收线程
        new Thread(() -> {
            try {
                ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
                while (true) {
                    Object obj = in.readObject();
                    if (obj instanceof Message) {
                        Message msg = (Message) obj;
                        System.out.println("[" + userId + " 收到消息] 来自 " + msg.getSenderId() + ": " + 
                                (msg.getType() == Message.TEXT ? msg.getContent() : "[图片]"));
                    }
                }
            } catch (Exception e) {
                System.out.println(userId + " 断开连接");
            }
        }).start();
    }
    
    private static void simulateDiamondMember(String userId) throws IOException {
        Socket socket = new Socket("localhost", 8888);
        ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
        out.writeObject(new Message(Message.REGISTER, userId, null, "diamond"));
        
        // 启动接收线程
        new Thread(() -> {
            try {
                ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
                while (true) {
                    Object obj = in.readObject();
                    if (obj instanceof Message) {
                        Message msg = (Message) obj;
                        System.out.println("[" + userId + " 收到消息] 来自 " + msg.getSenderId() + ": " + 
                                (msg.getType() == Message.TEXT ? msg.getContent() : "[图片]"));
                    }
                }
            } catch (Exception e) {
                System.out.println(userId + " 断开连接");
            }
        }).start();
    }
    
    private static void simulateSendTextMessage(String senderId, String receiverId, String message) throws IOException {
        Socket socket = new Socket("localhost", 8888);
        ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
        out.writeObject(new Message(Message.TEXT, senderId, receiverId, message));
        socket.close();
    }
    
    private static void simulateSendImageMessage(String senderId, String receiverId, byte[] imageData) throws IOException {
        Socket socket = new Socket("localhost", 8888);
        ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
        out.writeObject(new Message(Message.IMAGE, senderId, receiverId, imageData));
        socket.close();
    }
}